package com.ext.aic.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ext.aic.R;
import com.ext.aic.apis.user.UserInfoRequest;
import com.ext.aic.apis.user.UserInfoUpdateRequest;
import com.ext.aic.beans.UserInfo;
import com.ext.aic.beans.params.UserInfoParams;
import com.ext.aic.beans.response.UserInfoResponse;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.databinding.MineLayoutBinding;
import com.ext.aic.enums.ElementLocationType;
import com.ext.aic.handler.MineProductHandler;
import com.ext.aic.ui.CurrentTaskActivity;
import com.ext.aic.ui.SettingActivity;
import com.ext.aic.utils.RedirectUtils;
import com.ext.aic.utils.UserUtils;
import com.eyinfo.android_pure_utils.ebus.EBus;
import com.eyinfo.android_pure_utils.ebus.SubscribeEBus;

public class MineFragment extends BaseFragment {

    private MineLayoutBinding binding;
    private int currentPage = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mine_layout, container, false);
        binding = MineLayoutBinding.bind(view);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.editIv.setOnClickListener(this::onEditClick);
        binding.completeIv.setOnClickListener(this::onCompleteClick);
        binding.closeIv.setOnClickListener(this::onCloseClick);
        binding.currentTaskCv.setOnClickListener(this::onCurrentTaskClick);
        binding.settingIv.setOnClickListener(this::onSettingClick);
    }

    public void onSettingClick(View view) {
        RedirectUtils.getInstance().startActivity(getContext(), SettingActivity.class);
    }

    public void onCloseClick(View view) {
        binding.userNameTv.setVisibility(View.VISIBLE);
        binding.editIv.setVisibility(View.VISIBLE);
        binding.completeIv.setVisibility(View.GONE);
        binding.userNameEt.setVisibility(View.GONE);
        binding.closeIv.setVisibility(View.GONE);
    }

    public void onEditClick(View view) {
        binding.userNameEt.setText(binding.userNameTv.getText());
        binding.completeIv.setVisibility(View.VISIBLE);
        binding.userNameEt.setVisibility(View.VISIBLE);
        binding.closeIv.setVisibility(View.VISIBLE);
        binding.userNameTv.setVisibility(View.GONE);
        binding.editIv.setVisibility(View.GONE);
    }

    public void onCompleteClick(View view) {
        UserInfoParams params = new UserInfoParams();
        params.setDisplayName(binding.userNameEt.getText().toString());
        userInfoUpdateRequest.request(getContext(), this, params);
    }

    public void onCurrentTaskClick(View view) {
        RedirectUtils.getInstance().startActivity(getContext(), CurrentTaskActivity.class);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        EBus.getInstance().registered(this);
        userInfoRequest.request(getContext(), this);
        mineProductHandler.init(getContext(), this, binding.refreshListSrl, binding.publishListRv, ElementLocationType.mine, false);
    }

    private MineProductHandler mineProductHandler = new MineProductHandler() {
        @Override
        protected void onMineDataStatus(boolean empty) {
            if (empty) {
                binding.refreshListSrl.setVisibility(View.GONE);
                binding.emptyLl.setVisibility(View.VISIBLE);
            } else {
                binding.refreshListSrl.setVisibility(View.VISIBLE);
                binding.emptyLl.setVisibility(View.GONE);
            }
        }
    };

    private UserInfoRequest userInfoRequest = new UserInfoRequest() {
        @Override
        public void onRequestUserInfoSuccess(UserInfoResponse response) {
            binding.userNameTv.setText(response.getDisplayName());
        }
    };

    private UserInfoUpdateRequest userInfoUpdateRequest = new UserInfoUpdateRequest() {
        @Override
        public void onUserInfoUpdateSuccess(UserInfoResponse response) {
            binding.userNameTv.setText(response.getDisplayName());
            UserInfo userInfo = UserUtils.getInstance().getUserInfo();
            userInfo.setUserName(response.getDisplayName());
            UserUtils.getInstance().saveUserInfo(userInfo);
        }

        @Override
        public void onUserInfoUpdateEnd() {
            UserInfo userInfo = UserUtils.getInstance().getUserInfo();
            binding.userNameTv.setText(userInfo.getUserName());
            binding.userNameTv.setVisibility(View.VISIBLE);
            binding.editIv.setVisibility(View.VISIBLE);
            binding.completeIv.setVisibility(View.GONE);
            binding.userNameEt.setVisibility(View.GONE);
            binding.closeIv.setVisibility(View.GONE);
        }
    };

    @SubscribeEBus(receiveKey = EBusConstants.refreshMyPublishList)
    public void onRefreshMyPublishListEvent() {
        mineProductHandler.refresh();
    }
}
